using UnityEngine;
using System.Collections;

public class manifestante1 : MonoBehaviour
{
    int id = 21;
    float randomX;
    int randomSpeed = Random.Range(2, 4);
    private Vector2 screen;

    void Start()
    {
        this.screen.y = GameObject.Find("Main Camera").GetComponent<Camera>().orthographicSize;
        this.screen.x = this.screen.y * GameObject.Find("Main Camera").GetComponent<Camera>().aspect;

        randomX = Random.Range(this.screen.x * -0.7f, this.screen.x * 0.7f);
        this.transform.position = new Vector3(randomX, this.screen.y * -0.5f, -0.1f);
        this.gameObject.AddComponent("MeshRenderer");
        this.gameObject.AddComponent("MeshFilter");
        this.gameObject.AddComponent("BoxCollider");
        this.gameObject.GetComponent<BoxCollider>().size = new Vector3(this.screen.x * 0.12f, this.screen.x * 0.18f, 0);
        this.gameObject.AddComponent("Rigidbody");
        this.gameObject.GetComponent<Rigidbody>().useGravity = false;

        this.rigidbody.constraints = RigidbodyConstraints.FreezeAll;
        this.gameObject.tag = "manifestante1";

        Mesh mesh = Quad.Create(this.screen.x * 0.08f, this.screen.y * 0.2f);

        this.gameObject.GetComponent<MeshFilter>().mesh = mesh;
        this.gameObject.GetComponent<MeshRenderer>().material = AtlasManager.globalMaterial;

        this.gameObject.GetComponent<MeshFilter>().mesh.uv = new Vector2[]
        {
           new Vector2(AtlasManager.rects[id].xMin, AtlasManager.rects[id].yMax),
           new Vector2(AtlasManager.rects[id].xMax, AtlasManager.rects[id].yMax),
           new Vector2(AtlasManager.rects[id].xMax, AtlasManager.rects[id].yMin),
           new Vector2(AtlasManager.rects[id].xMin, AtlasManager.rects[id].yMin)
        };

        StartCoroutine(destroy());
    }

    void OnCollisionEnter(Collision c)
    {
    }

    void Update()
    {
        transform.Translate(0, 0.06f, 0);

        if (transform.position.y >= this.screen.y * -0.25f)
        {
            this.transform.position = new Vector3(transform.position.x, this.screen.y * -0.25f, transform.position.z);
        }

        this.gameObject.GetComponent<MeshFilter>().mesh.uv = new Vector2[]
        {
           new Vector2(AtlasManager.rects[id].xMin, AtlasManager.rects[id].yMax),
           new Vector2(AtlasManager.rects[id].xMax, AtlasManager.rects[id].yMax),
           new Vector2(AtlasManager.rects[id].xMax, AtlasManager.rects[id].yMin),
           new Vector2(AtlasManager.rects[id].xMin, AtlasManager.rects[id].yMin)
        };
    }

    void OnMouseDown()
    {
        if (id == 21)
        {
            gameProtesto.scoreManifestante1++;
            id = 23;
        }
    }

    IEnumerator destroy()
    {

        while (true)
        {
            yield return new WaitForSeconds(0.9f);
            Destroy(gameObject);
            gameProtesto.scoreManifestante2++;
        }
    }

}